<template>
   <el-dialog
     :visible.sync="isVisible"
     :title="title"
     width="40%"
     @close="closeDialog">
     <el-form
       ref="productForm"
       :model="productForm"
       :rules="productForm_rules"
       label-width="120px">
       <el-form-item prop="product_no" label="产品编号">
         <el-input v-model="productForm.product_no"  :validate-event="false" :disabled="isDisabled"></el-input>
       </el-form-item>
       <el-form-item prop="product_name" label="产品名称">
         <el-input v-model="productForm.product_name"  :validate-event="false"></el-input>
       </el-form-item>
       <el-form-item prop="specifications" label="产品规格">
         <el-input v-model="productForm.specifications"  :validate-event="false"></el-input>
       </el-form-item>
       <el-form-item label="工艺代号">
         <el-input v-model="productForm.process_code" :validate-event="false"></el-input>
       </el-form-item>
     </el-form>
     <span class="pl-5 colorRed">提示：带*的为必填内容</span>
     <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSubmit('productForm')">确 定</el-button>
        <el-button @click="isVisible = false">关 闭</el-button>
     </span>
   </el-dialog>
</template>

<script>
import {addProduct,updateProduct} from '@/api/product.js'
export default {
  name: "ProductDialog",
  data() {
    return {
      //控制对话框显示
      isVisible: this.isShow,
      isDisabled:false,
      // 表单
      productForm: {
        // product_id:"",
        product_no: "",
        product_name: "",
        specifications:"",
        process_code: "",        
      },
      //表单校验
      productForm_rules: {
        product_no: [
          {
            required: true,
            message: "请输入产品编号",
          },
        ],
         product_name:[
           {
            required: true,
            message: "请输入产品名称",
          },
        ],
         specifications: [
          {
            required: true,
            message: "请输入产品规格",
          },
        ],
      },
    }
  },
  props: {
    isShow: Boolean,
    dialogRow: Object,
    title:String
  },
  mounted() {
      if (this.title === "添加产品") {
       this.$nextTick(() => {
        this.$refs["productForm"].resetFields();
      });   
    } else {
       this.productForm = this.dialogRow;     
       this.isDisabled = true;   
    }
  },
  methods: {
    //表单提交
    onSubmit(productForm) {
      this.$refs[productForm].validate(async valid => {
        if (valid) {
              // add
          if (this.title === "添加产品") {
               await addProduct(this.productForm)
              this.$message.success('添加产品成功!')          
          } else {
            //edit
              delete this.productForm.sop_name
              await updateProduct(this.productForm)
              this.$message.success('修改产品成功!')
           }
            this.$refs["productForm"].resetFields(); //清空表单
            this.isVisible = false; //关闭对话框
            this.$emit("getDataList"); //刷新列表
        }
      });
    },
    //关闭对话框
    closeDialog() {
      this.$emit("closeDialog");
    },
  },
};
</script>

<style lang = "scss" scoped>


</style>
